package net.jielan188.hd.service.base;

import net.jielan188.hd.bean.base.MyBatisBaseI;
import net.jielan188.hd.mapper.base.BaseMapper;
import net.jielan188.hd.util.CommonUtils;
import net.jielan188.hd.util.Log;
import org.springframework.beans.factory.annotation.Autowired;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * 通用服务
 * @param <T>
 */
public class BaseService<T> {

    @Autowired
    private BaseMapper<T> baseMapper;
    @Autowired
    private MyBatisBaseI myBatisBaseI;

    /**
     * 分页
     * @param t
     * @return
     */
    public List<T> pageList(T t ){

        List<T> list = new ArrayList<>();
        try {

            String sql = myBatisBaseI.getPageSqlSql( t );
            List<HashMap<String,Object>> mapList = baseMapper.pageList( sql );
            for ( HashMap<String,Object> hashMap : mapList ){
                //实例化一个对象
                T newT = (T)t.getClass().newInstance();
                getDateFromMap( hashMap,newT );
                list.add( newT );
            }
        }catch ( Exception e ){
            Log.error( "处理数据发生异常" );
        }
        return list;
    }

    /**
     * 获取详情
     * @param t
     * @return
     */
    public T queryDetail( T t ){

        T newT = null;
        try {

            String sql = myBatisBaseI.getDeleteSql( t );
            HashMap<String,Object> detailMap = baseMapper.queryDetail( sql );

            newT = (T)t.getClass().newInstance();
            getDateFromMap( detailMap,newT );
        }catch ( Exception e ){
            Log.error( e.getMessage() );
        }

        return newT;
    }

    /**
     * 删除一个对象
     * @param t
     * @return
     */
    public Integer deleteObject(T t ){

        try {

            String sql = myBatisBaseI.getDeleteSql( t );
            return baseMapper.deleteObject( sql );
        }catch ( Exception e ){
            Log.error( e.getMessage() );
            return 0;
        }
    }

    /**
     * 添加一个对象
     * @param t
     * @return
     */
    public Integer saveObject(T t ){

        try {

            String sql = myBatisBaseI.getSaveAddSql( t );
            return baseMapper.saveObject( sql );
        }catch ( Exception e ){
            Log.error( e.getMessage() );
            return 0;
        }
    }

    /**
     * 更新一行记录
     * @param t
     * @return
     */
    public Integer updateObject(T t ){

        try {

            String sql = myBatisBaseI.getUpdateSql( t );
            return baseMapper.updateObject( sql );
        }catch ( Exception e ){
            Log.error( e.getMessage() );
            return 0;
        }
    }

    /**
     * 查询总数量
     * @param t
     * @return
     */
    public Integer getTotalCount(T t ){

        try {

            String sql = myBatisBaseI.getTotalCountSql( t );
            return baseMapper.getTotalCount( sql );
        }catch ( Exception e ){
            Log.error( e.getMessage() );
            return 0;
        }
    }

    /**
     * 封装数据
     * @param hashMap
     * @param t
     * @return
     * @throws Exception
     */
    private T getDateFromMap(HashMap<String,Object> hashMap, T t ) throws Exception {

        Class cls = t.getClass();
        Field[] fields = cls.getDeclaredFields();
        for ( Field field : fields ){
            if( hashMap.get( CommonUtils.camel4underline( field.getName() ) ) != null ){
                Method method = cls.getMethod( "set"+ CommonUtils.firstNameToLarge( field.getName() ),field.getType() );
                method.invoke( t, hashMap.get( CommonUtils.camel4underline( field.getName() ) ) );
            }
        }
        return t;
    }

}
